package com.yishusmt.mobsmt.serviceimpl;

import com.yishusmt.mobsmt.dto.base.Result;
import com.yishusmt.mobsmt.dto.request.UserRegisterReq;
import com.yishusmt.mobsmt.dto.response.UserRes;
import com.yishusmt.mobsmt.entity.UserEntity;
import com.yishusmt.mobsmt.enums.ResultCodeEnum;
import com.yishusmt.mobsmt.mapper.UserEntityMapper;
import com.yishusmt.mobsmt.mapper.UserInfoMapper;
import com.yishusmt.mobsmt.service.RegisterService;
import com.yishusmt.mobsmt.utils.DateUtil;
import com.yishusmt.mobsmt.utils.MD5Util;
import com.yishusmt.mobsmt.utils.RedisUtil;
import com.yishusmt.mobsmt.utils.TipsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.yishusmt.mobsmt.enums.ResultCodeEnum.*;

/**
 * 用户注册实现
 *
 * @author zhangyi
 * @date 2018/8/10 13:11
 */
@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserEntityMapper userEntityMapper;

    /**
     * 用户注册
     *
     * @param userRegisterReq
     * @return
     */
    @Override
    public Result register(UserRegisterReq userRegisterReq) {
//        条款同意是否
        if(userRegisterReq.getDisAgree() != null ? userRegisterReq.getDisAgree() : false){
            //验证码校验
            String code = redisUtil.get(userRegisterReq.getPhone()) != null ? redisUtil.get(userRegisterReq.getPhone()):"";
            if(code.equals(userRegisterReq.getCode().trim())){
                //删除缓存验证码
                redisUtil.del(userRegisterReq.getPhone());
                System.out.println("删除验证码");
                boolean userExist = checkUserInfo(userRegisterReq);
                //判断用户是否存在
                if(!userExist){
                    //存入用户
                    //有时间改为DTO和实体类之间的转化（反射）
                    UserEntity userEntity = new UserEntity();
                    userEntity.setName(userRegisterReq.getUsername());
                    String s = MD5Util.encryptString(userRegisterReq.getPassword());
                    userEntity.setPassword(s);
                    userEntity.setPhone(userRegisterReq.getPhone());
                    userEntity.setCreateTime(DateUtil.getCurrentDate());
                    userEntity.setUpdateTime(DateUtil.getCurrentDate());
                    int i = userEntityMapper.insertSelective(userEntity);
                    return 0 == i ? TipsUtil.returnTips(OPERATION_FAIL,null):TipsUtil.returnTips(OPERATION_SUCCESS,null);
                }else{
                    return TipsUtil.returnTips(USER_EXIST,null);
                }
            }else{
                return TipsUtil.returnTips(CODE_ERROR,null);
            }
        }else{
            return TipsUtil.returnTips(IS_ARGGER,null);
        }
    }

    /**
     * 查询用户信息
     *
     * @param phone
     * @return
     */
    @Override
    public Result selectNowUser(String phone) {
        UserRes userRes = userInfoMapper.selectUserSelective(phone);
        UserRes userInfo = userRes != null ? userRes : null;
        Result result = new Result();
        result.setStatus(ResultCodeEnum.OPERATION_SUCCESS.key());
        result.setData(userInfo);
        return result;
    }

    /**
     *  用户查重
     *
     * @param userRegisterReq
     * @return
     */
    public boolean checkUserInfo(UserRegisterReq userRegisterReq){
        final boolean[] checkUser = {false};
        String username = userRegisterReq.getUsername();
        String phone = userRegisterReq.getPhone();
        List<UserRes> userList = getUserList(userRegisterReq);
        userList.stream().forEach(userInfo ->
                checkUser[0] = userInfo.getName().equals(username)||userInfo.getPhone().equals(phone));
       return checkUser[0];
    }

    /**
     * 获取用户列表
     *
     * @param userRegisterReq
     * @return
     */
    public List<UserRes> getUserList(UserRegisterReq userRegisterReq){
        List<UserRes> userResList = userInfoMapper.selectListUser();
        return userResList;
    }

}
